package com.dw.message.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dw.apis.UserClient;
import com.dw.common.entitys.BaseResponse;
import com.dw.common.utils.ParseClientResultUtils;
import com.dw.message.mapper.MessageInfoMapper;
import com.dw.message.service.MessageUserService;
import com.dw.model.message.dtos.AddMessageDTO;
import com.dw.model.message.dtos.QueryDetailMessageDTO;
import com.dw.model.message.dtos.QueryMessageDTO;
import com.dw.model.message.entitys.MessageInfo;
import com.dw.message.service.MessageInfoService;
import com.dw.model.message.entitys.MessageUser;
import com.dw.model.message.vos.QueryMessageVO;
import com.dw.model.user.vos.GetUserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 聊天信息表(MessageInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-10-28 08:39:44
 */
@Service("messageInfoService")
public class MessageInfoServiceImpl extends ServiceImpl<MessageInfoMapper, MessageInfo> implements MessageInfoService {

    @Resource
    private MessageUserService messageUserService;

    @Resource
    private UserClient userClient;

    /**
     * 条件分页查询消息列表
     */
    @Override
    public List<QueryMessageVO> queryMessage(QueryMessageDTO queryMessageDTO) {
        List<MessageInfo> messageInfos;
        // 查询消息列表
        if(queryMessageDTO.getCreatedBy() != null){
            messageInfos = lambdaQuery().eq(MessageInfo::getCreatedBy, queryMessageDTO.getCreatedBy()).list();
        } else {
            messageInfos = lambdaQuery().eq(MessageInfo::getMessageType, 1).list();
        }
        // 分页查询
        List<List<MessageInfo>> partition = Lists.partition(messageInfos, queryMessageDTO.getPageSize());
        List<MessageInfo> messageInfoList = partition.get(queryMessageDTO.getCurrent() - 1);
        // 设置返回参数
        List<QueryMessageVO> collect = messageInfoList.stream().map(messageInfo -> {
            QueryMessageVO queryMessageVO = new QueryMessageVO();
            BeanUtils.copyProperties(messageInfo, queryMessageVO);
            // 调用狗屎的openfeign
            BaseResponse<GetUserVO> userName = userClient.getUserName(messageInfo.getCreatedBy());
            GetUserVO data = ParseClientResultUtils.parseClientResult(userName);
            queryMessageVO.setAvatar(data.getAvatar());
            queryMessageVO.setNickName(data.getNickname());
            queryMessageVO.setUpdatedBy(String.valueOf(messageInfo.getUpdatedBy()));
            // Long类型转为String方便前端接收
            queryMessageVO.setCreatedBy(String.valueOf(messageInfo.getCreatedBy()));
            queryMessageVO.setLeiId(String.valueOf(messageInfo.getLeiId()));
            return queryMessageVO;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 发送消息
     */
    @Override
    public boolean addMessage(AddMessageDTO addMessageDTO) {
        MessageInfo messageInfo = new MessageInfo();
        BeanUtils.copyProperties(addMessageDTO, messageInfo);
        // 设置消息类型
        messageInfo.setMessageType(1);
        if(addMessageDTO.getReceiveId() != null){
            messageInfo.setMessageType(2);
        }
        // 生成消息id
        long leiId = IdUtil.getSnowflake().nextId();
        messageInfo.setLeiId(leiId);
        // 设置创建时间
        messageInfo.setCreatedTime(DateTime.now());
        save(messageInfo);
        // 向关系表中添加数据
        MessageUser messageUser = new MessageUser();
        BeanUtils.copyProperties(addMessageDTO, messageUser);
        // 生成关系表id
        long l = IdUtil.getSnowflake().nextId();
        messageUser.setLeiId(l);
        // 设置其他参数
        messageUser.setMessageId(leiId);
        messageUser.setCreatedBy(Long.valueOf(addMessageDTO.getCreatedBy()));
        messageUserService.save(messageUser);
        return true;
    }

    /**
     * 查询私信内容
     */
    @Override
    public List<QueryMessageVO> queryDetailMessage(QueryDetailMessageDTO queryDetailMessageDTO) {
        // 查询消息id列表
        List<Long> idList = messageUserService.queryMessageIds(queryDetailMessageDTO.getReceiveId(), queryDetailMessageDTO.getCreatedBy());
        List<MessageInfo> messageInfos = idList.stream().map(id -> {
            return lambdaQuery().eq(MessageInfo::getLeiId, id)
                    .eq(MessageInfo::getMessageType, 2).one();
        }).collect(Collectors.toList());
        if(messageInfos.isEmpty()){
            return new LinkedList<>();
        }
        // 分页查询
        List<List<MessageInfo>> partition = Lists.partition(messageInfos, queryDetailMessageDTO.getPageSize());
        List<MessageInfo> messageInfoList = partition.get(queryDetailMessageDTO.getCurrent() - 1);
        // 设置返回参数
        List<QueryMessageVO> collect = messageInfoList.stream().map(messageInfo -> {
            QueryMessageVO queryMessageVO = new QueryMessageVO();
            BeanUtils.copyProperties(messageInfo, queryMessageVO);
            // 调用狗屎的openfeign
            BaseResponse<GetUserVO> userName = userClient.getUserName(messageInfo.getCreatedBy());
            GetUserVO data = ParseClientResultUtils.parseClientResult(userName);
            queryMessageVO.setAvatar(data.getAvatar());
            queryMessageVO.setNickName(data.getNickname());
            queryMessageVO.setUpdatedBy(String.valueOf(messageInfo.getUpdatedBy()));
            // Long类型转为String方便前端接收
            queryMessageVO.setCreatedBy(String.valueOf(messageInfo.getCreatedBy()));
            queryMessageVO.setLeiId(String.valueOf(messageInfo.getLeiId()));
            return queryMessageVO;
        }).collect(Collectors.toList());
        return collect;
    }
}

